package com.juli.v2xdemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;


public class Location_Service extends Service {

    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new MyAMapLocationListener();
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    public Location_Service() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    int FixInterval;

    @Override
    public void onCreate() {
        super.onCreate(); //声明AMapLocationClient类对象
        FixInterval = 20;
        init();
    }

    private void init() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(false);
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        mLocationOption.setInterval(FixInterval * 1000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }

    private class MyAMapLocationListener implements AMapLocationListener {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    if (ALL_CONFIG.Debug) {
                        Log.e("位置：", aMapLocation.getAddress());
                    }
                    ALL_CONFIG.Gaode = aMapLocation.getAddress();
                    ALL_CONFIG.  ShiQU=aMapLocation.getCity();
                    ALL_CONFIG.  Weizhi=aMapLocation.getAddress();
                    float bearing = aMapLocation.getBearing();
                    Log.e("车辆航向角Heading2", ""+bearing);

                    //维度
                    double latitude = aMapLocation.getLatitude();
                    ALL_CONFIG.Latitude = String.valueOf(latitude);
                    //经度
                    double Longitude = aMapLocation.getLongitude();
                    ALL_CONFIG.Longitude = String.valueOf(Longitude);
                    Log.e("位置：", " ALL_CONFIG.Latitude"+ ALL_CONFIG.Latitude+"，ALL_CONFIG.Longitude"+  ALL_CONFIG.Longitude);
//                    setWorkerTrack(String.valueOf(Longitude), String.valueOf(latitude));
                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                }
            }
        }
    }

    /**
     * 上传位置
     */
//    public void setWorkerTrack(String JD, String WD) {
//
//        API.setWorkerTrack(JD, WD, new Okhttp.Objectcallback() {
//            @Override
//            public void onsuccess(String st) {
//                if (ALL_CONFIG.Debug) {
//                    Log.e("711setWorkerTrack", st);
//                }
//
//                Location_Result sign_bean = JSONUtils.parseJSON(st, Location_Result.class);
//                if (sign_bean.getResult().equals("1")) {
//                    //版本检查成功
//                    Log.e("711setWorkerTrack", "位置上传成功");
//                } else {
//                    //版本检查失败
//                    Log.e("711setWorkerTrack", "位置上传失败");
//                }
//            }
//
//            @Override
//            public void onFalia(int code, String errst) {
//                Log.e("7788", errst);
//            }
//        });
//    }
}
